import {BaseRouters, SystemRouters, CmsRouters} from "@/router";
import {setStore} from '@/utils/store'

const system = {
    state: {
        routers: null,
        addRouters: []
    },

    mutations: {
        SET_ROUTERS: (state, routers) => {
            //业务路由
            state.addRouters = routers

            //将业务路由与公共路由合并
            state.routers = BaseRouters.concat(routers)
        }
    },

    actions: {
        /**
         * 生成路由表
         * @param commit
         * @param data
         * @returns {Promise<any>}
         * @constructor
         */
        GeneratorRouters({ commit }, data) {
            return new Promise((resolve, reject) => {
                commit('SET_ROUTERS', data)
                resolve()
            })
        },

        /**
         * //切换路由表
         * @param dispatch
         * @param commit
         * @param state
         * @param routerType
         * @returns {Promise<any>}
         * @constructor
         */
        ChangeRouters({ dispatch, commit, state }, routerType) {
            return new Promise((resolve, reject) => {
                if('CMS' === routerType) {
                    commit('SET_ROUTERS', CmsRouters)
                } else {
                    commit('SET_ROUTERS', SystemRouters)
                }
                setStore({ name: 'ROUTER_TYPE', content: routerType })

                //刷新页面，达到清空原有路由表
                window.location = ''

                resolve()
            })
        }
    }
}

export default system
